package com.compomics.prioritizer.db.accessors;


import java.util.HashMap;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 31-okt-2be.proteomics.priority008
 * Time: 8:57:46
 * To change this template use File | Settings | File Templates.
 */
public class Instrument extends InstrumentTableAccessor {

    /**
     * Default constructor.
     */
    public Instrument() {
        super();
    }

    /**
     * Wrapper for the superclass constructor.
     *
     * @param aParams   HashMap with the parameters.
     */
    public Instrument(HashMap aParams) {
        super(aParams);
    }

    /**
     * This constructor reads the instrument from a resultset. The ResultSet should be positioned such that
     * a single row can be read directly (i.e., without calling the 'next()' method on the ResultSet). <br />
     * The columns should be in this order: <br />
     * Column 1: instrument ID <br />
     * Column 2: name of the instrument <br />
     * Column 3: the description for the instrument <br />
     * Column 4: l_userid  <br/>
     * Column 4: username <br />
     * Column 5: creationdate <br />
     * Column 6: modificationdate.
     *
     * @param   aRS ResultSet to read the data from.
     * @exception java.sql.SQLException    when reading the ResultSet failed.
     */
    public Instrument(ResultSet aRS) throws SQLException {
        iInstrumentid = aRS.getLong(1);
        iName = aRS.getString(2);
        iDescription = aRS.getString(3);
        iUsername = aRS.getString(4);
        iCreationdate = (java.sql.Timestamp)aRS.getObject(5);
        iModificationdate = (java.sql.Timestamp)aRS.getObject(6);
    }

    public int getLowestPriority(Connection aConn) throws SQLException{
        PreparedStatement prep = aConn.prepareStatement("select priority from project where l_instrumentid_accepted = ? and priority != -1 and priority != 0");
        prep.setLong(1,this.iInstrumentid);
        ResultSet rs = prep.executeQuery();
        int priority = 0;
        while(rs.next()) {
            if(priority < rs.getInt(1)){
                priority = rs.getInt(1);
            }
        }
        rs.close();
        prep.close();
        return priority;
    }

    /**
     * This methods reads all instruments from the Instrument table.
     *
     * @param aConn Connection to read the instruments from.
     * @return  Instrument[] with the instruments in the 'Instrument' table.
     * @throws SQLException when the retrieving of the instruments went wrong.
     */
    public static Instrument[] getAllInstruments(Connection aConn) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from instrument");
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Instrument(rs));
        }
        rs.close();
        prep.close();
        Instrument[] lInstruments = new Instrument[v.size()];
        v.toArray(lInstruments);

        return lInstruments;
    }

    /**
     * This methods reads all instrumentss from the Instrument table that have a
     * calibrated differential standard deviation.
     *
     * @param aConn Connection to read the instruments from.
     * @param aId Connection to read the instruments from.
     * @return  Instrument with the instruments in the 'Instrument' table.
     * @throws SQLException when the retrieving of the instruments went wrong.
     */
    public static Instrument getInstrumentById(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from instrument where instrumentid = ?");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Instrument lInstrument = null;
        while(rs.next()) {
            lInstrument = new Instrument(rs);
        }
        rs.close();
        prep.close();

        return lInstrument;
    }

    /**
     * Returns a String representation for this Instrument.
     *
     * @return  String  with the String representation for this Instrument.
     */
    public String toString() {
        return this.iName;
    }

}
